﻿using System;
using System.Collections.Generic;
using System.Data;

using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client; 
//using Oracle.DataAccess.Types;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Oracle;

namespace DBQuery
{
    /// <summary>
    /// Summary description for FeedSubscriptionTable
    /// </summary>
    public static class FeedSubscriptionTable
    {
        public static int AddFeedSubscription(int iFeedSourceId, int iUserRoleId)
        {
            return AddFeedSubscription(new DBLib(), iFeedSourceId, iUserRoleId);
        }
        public static int AddFeedSubscription(DBLib dbl, int iFeedSourceId, int iUserRoleId)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_id";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_FEED_SOURCE_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iFeedSourceId;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_P_ICP_USER_ROLE_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iUserRoleId;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CREATE_BY";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = ICPUser.ID;

            String sxQuery = "PR_INSERT_T_FEED_SUBSCRIPTION";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());
            
            return Convert.ToInt32(lsParams[0].Value);
        }

        public static void DeleteFeedSubscription(int iFeedSourceId, int iUserRoleId)
        {
            DeleteFeedSubscription(new DBLib(), iFeedSourceId, iUserRoleId);
        }
        public static void DeleteFeedSubscription(DBLib dbl, int iFeedSourceId, int iUserRoleId)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmGenID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iFeedSourceId;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmRoleID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iUserRoleId;

            //String sxQuery = 
            //    "delete from T_FEED_SUBSCRIPTION where FEED_SOURCE_ID = :prmGenID and P_ICP_USER_ROLE_ID = :prmRoleID";

            //dbl.ExecuteNonQuery(sxQuery, lsParams.ToArray());            

            String sxQuery = String.Format(
                "delete from T_FEED_SUBSCRIPTION where FEED_SOURCE_ID = {0} and P_ICP_USER_ROLE_ID = {1}",
                iFeedSourceId, iUserRoleId);

            dbl.ExecuteNonQuery(sxQuery);
        }

    }
}